<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$uniacid=$_SESSION['uniacid'];
$return['set']=$this->plugin_setting('basic');
$mid=$_SESSION['uid'];
if($_RQ['op']=='main'){
	$id=$_RQ['id'];
	if(!$id){
		exi('请选择活动','error','jy_weishop_cutprice/index/');
	}
	$act = pdo_get('jy_weishop_cutprice_act',array('id'=>$id));
	$act['images']=json_decode($act['images'],true);
	$act['goods']=pdo_get('jy_weishop_goods',array('id'=>$act['goodsid']));
	//判断状态
	$status=$act['status'];
	if($act['starttime']>time()){
		$status=1;
		

	}
	if($act['endtime']<time()){
		$status=3;
	}
	if($act['total']<1){
		$status=4;
		pdo_update('jy_weishop_cutprice_act',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}
	//限购
	//未超时的所有订单
	$sql = "SELECT SUM(num) FROM " . tablename('jy_weishop_cutprice_order') . " WHERE uniacid=:uniacid AND actid=:actid AND istimeout=0";
    $number = pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid'],":actid" =>$id));	
	$canbuy=$act['buynum']-$number['SUM(num)'];
	//$return['intDiff']=floor((time()-$act['starttime'])/($act['cutprice'] * 60*60));	
	//对现价的计算  起拍价-（（当前时间-开始时间）/降价力度时间  取整 ）*降价力度价格
	$lilun_price=floor((time()-$act['starttime'])/($act['cuttime'] * 60))*$act['cutprice'];
	$chajia=$act['startprice']-$act['stopprice'];
	if($lilun_price>=$chajia){
		$act['nowprice']=$act['stopprice'];
	}else{
		$act['nowprice']=$act['startprice']-$lilun_price;
	}
	//计算降价结束时间
//	print_r($chajia/$act['cutprice']*$act['cuttime'] * 60+$act['starttime']-time());exit;
	if($chajia/$act['cutprice']*$act['cuttime'] * 60+$act['starttime']>time()){
		$return['intDiff']=$chajia/$act['cutprice']*$act['cuttime'] * 60+$act['starttime']-time();
	}else{
		$return['intDiff']=0;
	}
	//print_r($return['intDiff'].'dfdd');exit;
	//竞拍成功的订单
	$orders=pdo_getall('jy_weishop_cutprice_order',array('uniacid'=>$uniacid,'ispay'=>'1','actid'=>$act['id']));
	if($orders){
		foreach($orders as &$order){
			$order['member']=pdo_get('core_members',array('uniacid'=>$uniacid,'id'=>$mid));
		}
	}
	$act['image']=$act['images']['0'];
	$orders['count']=count($orders);
	$return['orders']=$orders;
	$return['status']=$status;
	$return['act']=$act;
	$return['num']=1;
	$return['canbuy']=$canbuy;
	set_html('title','拍卖活动-'.$act['title']);
	exi($return);
}

if($_RQ['op']=='check'){
	$post=get_postdata();
	$order=pdo_get('jy_weishop_cutprice_order',array('uniacid'=>$uniacid,'mid'=>$_SESSION['uid'],'status'=>0,'ispay'=>0,'actid'=>$post['actid']));
	if($order){
		$return['type']=2;
	}else{
		$return['type']=1;
	}
	$return['actid']=$post['actid'];
	$return['price']=$post['price'];
	$return['num']=$post['num'];
	exi($return);
}